﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PMSA.Framework.Log;

public partial class Admin_Uc_News_EditNews : System.Web.UI.UserControl
{
    private int newsid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ni"] != null)
        {
            try
            {
                newsid = int.Parse(Request.QueryString["ni"].ToString());
            }
            catch
            {
                CLogManager.Write("News.Edit.PageLoad", "newsid not a number: " + Request.QueryString["ni"].ToString(), "ADMIN");
            }
        }
        if (!IsPostBack)
        {
            CreateDDLGroupNews();
            GetDetailNews(newsid);
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        News obj = new News();
        string title = txtTitle.Text;
        string description = txtDescription.Text;
        string content = txtContent.Text;
        
        string thumbnail = photos1.Value;
        
        int groupNewsId = int.Parse(ddlGroupNews.SelectedValue);
        int status = int.Parse(txtStatus.Text);
        int isactive = cbxIsActive.Checked == true ? 1 : 0;

        string _photoNull = "../Img/layout/sample_large.jpg";
        string _photoLink = "../Admin/Upload/Image/News/";
        string _photo1 = _photoNull;
        if (!String.IsNullOrEmpty(photos1.Value))
        {
            _photo1 = _photoLink + photos1.Value;
        }
        obj = obj.Update(newsid, title, _photo1, content, description, groupNewsId, status, isactive);
        if (obj != null)
        {
            Response.Write("<script type='text/javascript'>alert('Edit Successful !');</script>");
        }
    }
    private void GetDetailNews(int newsid)
    {
        try
        {
            News obj = new News();
            obj = obj.Detail(newsid);
            txtTitle.Text = obj.Title;
            txtDescription.Text = obj.Description;
            txtContent.Text = obj.Content;
            ddlGroupNews.SelectedValue = obj.GroupNewsId.ToString();
            txtStatus.Text = obj.Status.ToString();
            cbxIsActive.Checked = obj.IsActive == 1 ? true : false;
            photos1.Value = obj.Thumbnail;
        }
        catch (Exception ex)
        {
            CLogManager.Write("Brand.Edit.GetDetailBrand", ex.ToString(), "ADMIN");
        }
    }
    private void CreateDDLGroupNews()
    {
        try
        {
            GroupNews obj = new GroupNews();
            IList<GroupNews> list = obj.List();
            ddlGroupNews.DataValueField = "Id";
            ddlGroupNews.DataTextField = "Name";
            ddlGroupNews.DataSource = list;
            ddlGroupNews.DataBind();
        }
        catch (Exception ex)
        {
            CLogManager.Write("News.Edit.CreateDDLGroupNews", ex.ToString(), "ERR");
        }
    }
}